<?php
namespace app\controller;

use app\BaseController;
use liliuwei\think\Auth;
use EasyDingTalk\Application;
use think\facade\Session;
use think\facade\Db;
class Classes extends BaseController
{
    protected $getpath="";
    protected $Ggid=0;
    public function __construct(){
        $this->getpath=strtolower(request()->controller())."/".strtolower(request()->action());
        checkuser();
    }
    public function rClassessToOption($cint=0){

    }
    public function index(){
        return "后台管理系统，非公司人员勿入！";
    }
    public function classes($cid=0){
        $gid=request()->param("gid")*1;
        $cid>0?$gid=$cid:'';
        $gid<1?$gid=0:'';
        return view("classes/classes")->assign("gid",$gid);
    }

    public function tagsform(){//权限添加页面
        $pathid=request()->param("pathid")*1;
        $ty=request()->param("ty")*1;
        $gid=request()->param("gid")*1;
        $cids = array_merge(r_classes(420)->toArray(),r_classes(421)->toArray());
        if($gid>0){
            //修改程序
            $rule=r_rule2($gid);
            if($rule){
                return view("classes/tagsform")->assign(["id"=>$pathid,'ty'=>$ty,'rule'=>$rule,'gid'=>$gid,'cids'=>$cids,'data'=>['cid'=>$rule['cid']]]);
            }
        }
        return view()->assign(["id"=>$pathid,'ty'=>$ty,'gid'=>$gid,'rule'=>'','cids'=>$cids,'data'=>['cid'=>0]]);
    }
    public function authruleedit(){
        $id=request()->param("gid");
        $pathid=request()->param("pathid");
        $tags=request()->param("tags");
        $rule=request()->param("rule");
        $sortid=request()->param("sortid");
        $file=request()->param("file");
        $cid=request()->param("cid");
        $content=request()->param("content");
        $re=DB::name("classes")->where("id",$id)->find();
        if($re){
            $up=DB::name("classes")->where("id",$id)->update(['title'=>$tags,'file'=>$file,'rule'=>$rule,'sortid'=>$sortid,'cid'=>$cid,'content'=>$content]);
            if($up){
                echojson([],0,"修改成功");
            }else{
                echojson([],1,"修改失败");
            }
        }else{
            echojson([],1,"记录不存在");
        }
    }
    public function tagssave(){//保存权限添加
        $tags=request()->param("tags");
        $pathid=request()->param("pathid");
        $sortid=request()->param("sortid");
        $file=request()->param("file");
        $rule=request()->param("rule");
        $cid=request()->param("cid");
        $content=request()->param("content");
        $re=DB::name("classes")->where("name",$tags)->find();
        if($tags==""){
            $re=false;
        }
        if(!$re){
            $arr=[
                'title'=>$tags,
                'pathid'=>$pathid,
                'sortid'=>$sortid,
                'file'=>$file,
                'rule'=>$rule,
                'cid'=>$cid,
                'content'=>$content,
            ];
            $re=DB::name("classes")->insert($arr);
            if($re){
                echojson([],0,"添加成功");
            }else{
                echojson([],1,"添加失败");
            }
        }else{
            echojson([],1,"重复记录");
        }

    }
    public function listdata(){//分类列表数据
        $gid=request()->param("gid")*1;
        if($gid>0){$this->Ggid=$gid;}
        echojson($this->classes2($gid));
    }
    protected function classes2($id){
        $rearr2=[];
        $res2=DB::name("classes")->where("pathid",$id)->where("status",1)->order("sortid desc")->select();
        if(count($res2)<1){
            $res2=DB::name("classes")->where("cid",$id)->where("status",1)->order("sortid desc")->select();
        }
        if($res2){
            $temarr=[];
            foreach ($res2 as $v2){
                $temarr['title']=$v2['title'].":".$v2['id'];
                $temarr['id']=$v2['id'];
                $temarr['checked']=false;
                $temarr['spread']=false;
                if($this->Ggid>0){
                    $temarr['spread']=true;
                }
                $temarr['classty']=$v2['classty'];
                $temarr['children']=$this->classes2($v2['id']);
                $rearr2[]=$temarr;
            }
            return $rearr2;
        }
    }
}
